<?php

namespace app\api\validate;

use app\common\business\Department;
use app\common\business\WorkType;
use app\common\lib\Num;
use app\common\lib\Str;
use think\Validate;

class Work extends Validate
{
    protected $rule = [
        'type' => 'require|number|checkType',
        'name' => 'require',
        'start_time' => 'require',
        'population' => 'require|checkPopulation',
        'valuation' => 'require|checkValuation',
        'price' => 'require|checkPrice',
        'duration' => 'require|checkDuration',
        'address' => 'require',
        'longitude' => 'require',
        'latitude' => 'require',
        'describe' => 'require',
        'attachment' => 'require',
        'is_enterprise' => ['require', "in" => "1,2"],
        'department_id' => 'require|checkDepartment'
    ];

    protected $message = [
        'type.require' => '类型必须',
        'type.number' => '类型错误',
        'name' => '零活名称必须',
        'start_time' => '开工时间必须',
        'population.require' => '用工人数必须',
        'population.number' => '用工人数错误',
        'valuation.require' => '计价方式必须',
        'valuation.number' => '计价方式错误',
        'price.require' => '单价必须',
        'price.number' => '单价错误',
        'duration.require' => '预估工期必须',
        'duration.number' => '预估工期错误',
        'address' => '工作地址必须',
        'longitude' => '工作地址经度必须',
        'latitude' => '工作地址纬度必须',
        'describe' => '零活描述必须',
        'attachment' => '工作环境及操作规范文件必须',
        'is_enterprise.require' => '是否企业发布必须',
        'is_enterprise.in' => '是否企业发布数值错误',
        'department_id.require' => '人资公司必须',
    ];

    protected function checkType($value, $rule, $data = [])
    {
        try {
            (new WorkType())->checkValidValue($value);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        return true;
    }

    protected function checkPopulation($value, $rule, $data = [])
    {
        if(!Num::checkIsPositiveInteger($value)){
            return '用工人数至少1人，且必须为整数';
        }
        return true;
    }

    protected function checkValuation($value, $rule, $data = [])
    {
        $checkArr = array_keys(config('work.valuation'));
        if(!in_array($value,$checkArr)){
            return '计价方式错误';
        }
        return true;
    }

    protected function checkPrice($value, $rule, $data = [])
    {
        if(!Num::checkIsDecimal2($value)){
            return '单价金额错误';
        }
        return true;
    }

    protected function checkDuration($value, $rule, $data = []){
        if(!Num::checkIsDecimal2($value)){
            return '预估工期错误';
        }
        return true;
    }

    protected function checkDepartment($value, $rule, $data = []){
        $departmentInfo = (new Department())->getNormalById($value);
        if (!$departmentInfo) {
            return '该人资公司不存在';
        }
        return true;
    }

    protected $scene = [
        'save'=> ['type','name','start_time','population','valuation','price','duration','address','longitude','latitude','describe','attachment','is_enterprise','department_id'],
    ];
}